<template>
  <div class="user-info">
    <!-- 个人信息页面 -->
    <el-form ref="form" label-width="220px" :model="userInfo">
      <!-- 手机 -->
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="userInfo.mobile" style="width:220px" />
      </el-form-item>
      <!-- 入职日期 -->
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="userInfo.timeOfEntry" type="date" class="inputW" value-format="yyyy-MM-dd" />
      </el-form-item>
      <!-- 员工照片 -->
      <el-form-item label="员工头像">
        <!-- 放置上传图片 -->
        <upload v-model="userInfo.staffPhoto" />
      </el-form-item>
      <!-- 保存个人信息 -->
      <el-form-item>
        <el-button type="primary" @click="save">保存更新</el-button>
        <el-button @click="$router.back()">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { getUserDetailById, saveUserDetailById } from '@/api/user.js'
export default {
  data() {
    return {
      userId: this.$route.query.id,
      userInfo: {
        mobile: '',
        timeOfEntry: '',
        staffPhoto: ''
      },
      rules: {
        mobile: [{ required: true, message: '手机号不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.loadUserDetailById()
  },
  methods: {
    async loadUserDetailById() {
      try {
        const res = await getUserDetailById(this.userId)
        console.log('getUserDetailById', res)
        this.userInfo = res.data
      } catch (err) {
        console.log('getUserDetailById', err)
      }
    },
    async saveUserDetailByIdFn() {
      const res = await saveUserDetailById(this.userInfo)
      console.log(res)
      this.$message.success('修改成功')
    },
    save() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.saveUserDetailByIdFn()
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.user-info {
  padding-top: 20px;
}
</style>
